package com.molichuxing.framework.utils;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;


/**
 * @author: zph
 * @create_date: 2019年08月13日
 **/
public class ObjectCastUtil {
    /**
     * obj转为String型
     *
     * @param obj
     * @return
     */
    public static String castString(Object obj) {
        return castString(obj, null);
    }

    /**
     * object转成String类型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static String castString(Object obj, String defaultValue) {
        return obj != null ? String.valueOf(obj) : defaultValue;
    }

    /**
     * object转成double类型
     *
     * @param obj
     * @return
     */
    public static double castDouble(Object obj) {
        return castDouble(obj, 0);
    }

    /**
     * object转成double类型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static double castDouble(Object obj, double defaultValue) {
        double value = defaultValue;
        if (obj == null) {
            return value;
        }

        String strValue = castString(obj);
        if (StringUtils.isNotEmpty(strValue)) {
            try {
                value = Double.parseDouble(strValue);
            } catch (NumberFormatException e) {
                value = defaultValue;
            }
        }
        return value;
    }


    /**
     * Object转成long型
     *
     * @param obj
     * @return
     */
    public static long castLong(Object obj) {
        return castLong(obj, 0);
    }

    /**
     * Object转成long型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static long castLong(Object obj, long defaultValue) {
        long value = defaultValue;
        if (obj == null) {
            return value;
        }
        String strValue = castString(obj);
        if (StringUtils.isNotEmpty(strValue)) {
            try {
                value = Long.parseLong(strValue);
            } catch (NumberFormatException e) {
                value = defaultValue;
            }
        }
        return value;
    }

    /**
     * Object转成Long型
     *
     * @param obj
     * @return
     */
    public static Long castLongPackType(Object obj) {
        return castLongPackType(obj, null);
    }

    /**
     * Object转成Long型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static Long castLongPackType(Object obj, Long defaultValue) {
        Long value = defaultValue;
        if (null == obj) {
            return value;
        }
        String strValue = castString(obj);
        if (StringUtils.isNotEmpty(strValue)) {
            try {
                value = Long.parseLong(strValue);
            } catch (NumberFormatException e) {
                value = defaultValue;
            }
        }
        return value;
    }

    /**
     * Object转成int型，Object为空返回0
     *
     * @param obj
     * @return
     */
    public static int castInt(Object obj) {
        return castInt(obj, 0);
    }

    /**
     * Object转成int型，Object为空返回0
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static int castInt(Object obj, int defaultValue) {
        int value = defaultValue;
        if (null == obj) {
            return value;
        }
        String strValue = castString(obj);
        if (StringUtils.isNotEmpty(strValue)) {
            try {
                value = Integer.parseInt(strValue);
            } catch (NumberFormatException e) {
                value = defaultValue;
            }
        }
        return value;
    }

    /**
     * Object转成Integer型
     *
     * @param obj
     * @return
     */
    public static Integer castInteger(Object obj) {
        return castInteger(obj, null);
    }

    /**
     * Object转成Integer型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static Integer castInteger(Object obj, Integer defaultValue) {
        Integer value = defaultValue;
        if (null == obj) {
            return value;
        }
        String strValue = castString(obj);
        if (StringUtils.isNotEmpty(strValue)) {
            try {
                value = Integer.parseInt(strValue);
            } catch (NumberFormatException e) {
                value = defaultValue;
            }
        }
        return value;
    }

    /**
     * Object转成boolean型
     *
     * @param obj
     * @return
     */
    public static boolean castBoolean(Object obj) {
        return castBoolean(obj, false);
    }

    /**
     * Object转成boolean型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static boolean castBoolean(Object obj, boolean defaultValue) {
        boolean value = defaultValue;
        if (obj != null) {
            value = Boolean.parseBoolean(castString(obj));
        }
        return value;
    }

    /**
     * Object转成boolean型
     *
     * @param obj
     * @return
     */
    public static Boolean castBooleanNull(Object obj) {
        return castBooleanNull(obj, null);
    }

    /**
     * Object转成boolean型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static Boolean castBooleanNull(Object obj, Boolean defaultValue) {
        Boolean value = defaultValue;
        if (obj != null) {
            value = Boolean.parseBoolean(castString(obj));
        }
        return value;
    }

    /**
     * Object（yyyy-MM-dd HH:mm:ss）转成LocalDateTime型
     *
     * @param obj
     * @return
     */
    public static LocalDateTime castLocalDateTime(Object obj) {
        return castLocalDateTime(obj, null);
    }

    /**
     * Object（yyyy-MM-dd HH:mm:ss）转成LocalDateTime型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static LocalDateTime castLocalDateTime(Object obj, LocalDateTime defaultValue) {
        if (null == obj) {
            return defaultValue;
        }

        String dateStr = castString(obj);
        if (StringUtils.isBlank(dateStr)) {
            return defaultValue;
        }

        return DateUtil.parseDateTime(dateStr);
    }

    public static LocalDateTime castLocalDateTimeStart(Object obj) {
        try {
            LocalDateTime dateTime = castLocalDateTime(obj);
            return dateTime == null ? null : DateUtil.getDayStart(dateTime);
        } catch (DateTimeException e) {
            LocalDate date = castLocalDate(obj);
            return date == null ? null : DateUtil.getDayStart(date);
        }
    }

    public static LocalDateTime castLocalDateTimeEnd(Object obj) {
        try {
            LocalDateTime dateTime = castLocalDateTime(obj);
            return dateTime == null ? null : DateUtil.getDayEnd(dateTime);
        } catch (DateTimeException e) {
            LocalDate date = castLocalDate(obj);
            return date == null ? null : DateUtil.getDayEnd(date);
        }
    }

    /**
     * Object转成LocalDate型
     *
     * @param obj
     * @return
     */
    public static LocalDate castLocalDate(Object obj) {
        return castLocalDate(obj, null);
    }

    /**
     * Object转成LocalDate型
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static LocalDate castLocalDate(Object obj, LocalDate defaultValue) {
        if (null == obj) {
            return defaultValue;
        }

        String dateStr = castString(obj);
        if (StringUtils.isBlank(dateStr)) {
            return defaultValue;
        }

        return DateUtil.parseDate(dateStr);
    }

    /**
     * Object（yyyy-MM-dd）获取开始时间
     *
     * @param obj
     * @return
     */
    public static LocalDateTime getDayStart(Object obj) {
        return getDayStart(obj, null);
    }

    /**
     * Object（yyyy-MM-dd）获取开始时间
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static LocalDateTime getDayStart(Object obj, LocalDateTime defaultValue) {
        LocalDateTime value = defaultValue;
        if (obj != null && StringUtils.isNotBlank(obj.toString())) {
            LocalDateTime localDateTime = castLocalDate(obj).atStartOfDay();
            value = DateUtil.getDayStart(localDateTime);
        }
        return value;
    }

    /**
     * Object（yyyy-MM-dd）获取开始时间
     *
     * @param obj
     * @return
     */
    public static LocalDateTime getDayEnd(Object obj) {
        return getDayEnd(obj, null);
    }

    /**
     * Object（yyyy-MM-dd）获取开始时间
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static LocalDateTime getDayEnd(Object obj, LocalDateTime defaultValue) {
        LocalDateTime value = defaultValue;
        if (obj != null) {
            LocalDateTime localDateTime = castLocalDate(obj).atStartOfDay();
            value = DateUtil.getDayEnd(localDateTime);
        }
        return value;
    }


    @SuppressWarnings("rawtypes")
    public static List castList(Object obj) {
        List value = null;
        if (obj != null) {
            value = (List) obj;
            if (value.size() == 0) {
                value = null;
            }
        }
        return value;
    }

    public static <T> T castEmun(Object obj, Class<T> clz) {
        if (clz.isInstance(obj)) {
            return clz.cast(obj);
        }
        return null;
    }
}
